package com.mh.service.impl;

import com.mh.dto.R;
import com.mh.dto.vo.GoodsDataReturnDto;
import com.mh.dto.vo.GoodsDataVo;
import com.mh.dto.vo.GoodsTipsVo;
import com.mh.pojo.GoodsInfos;
import com.mh.service.GoodsInfosService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:2024/10/20
 * author:zmh
 * description: 商品服务类接口实现类
 **/

@Service
@Slf4j
@RequiredArgsConstructor
public class GoodsInfosServiceImpl implements GoodsInfosService {

    private final MongoTemplate mongoTemplate;

    /**
     * 商品列表视图组件获取商品数据
     * @param goodsDataVo 商品组件展示的位置和值以及分页参数封装对象，展示的类型，如index首页
     *                 传入类型【index-首页推荐】 ，【type-分类】，
     *                 【search-搜索】，【store-店铺】
     * @return
     */
    @Override
    public R<GoodsDataReturnDto> getGoodsData(GoodsDataVo goodsDataVo) {
        Query query = null;
        // 根据showType判断如何过滤商品数据，构造查询参数
        if(goodsDataVo.getShowType().equals("index")){
            // 首页推荐商品
            query = new Query(Criteria.where("recommend").is(true));
        }else if(goodsDataVo.getShowType().equals("type")){
            // 根据类型过滤
            query = new Query(Criteria.where("type").is(goodsDataVo.getTypeValue()));
        }else if(goodsDataVo.getShowType().equals("search")){
            // 根据goodsName模糊查询
            query = new Query(Criteria.where("goodsName").regex(goodsDataVo.getTypeValue()));
        }else if(goodsDataVo.getShowType().equals("store")){
            query = new Query(Criteria.where("ownerStoreId").is(goodsDataVo.getTypeValue()));
        }else{
            query = new Query();
        }

        // 查询总数数
        long count = mongoTemplate.count(query, GoodsInfos.class);

        // 添加分页条件
        int page = goodsDataVo.getPage() != null ? goodsDataVo.getPage() - 1 : 0;
        int size = goodsDataVo.getSize() != null ? goodsDataVo.getSize() : 8; // 不传默认每页8条

        if(page != 0){ // 设置分页skip值
            page = page * size;
        }

        // 设置分页
        query.skip(page).limit(size);
        List<GoodsInfos> goodsInfos = mongoTemplate.find(query, GoodsInfos.class);
        // 构造DTO对象返回
        GoodsDataReturnDto goodsDataReturnDto = new GoodsDataReturnDto();
        goodsDataReturnDto.setCount((int)count);
        goodsDataReturnDto.setGoodsInfos(goodsInfos);
        return R.success(goodsDataReturnDto);
    }

    /**
     * 根据商品名称获取商品信息（模糊搜索 - 搜索提示）
     * @param goodsName 商品名称
     * @return ·
     */
    @Override
    public R<List<GoodsInfos>> getGoodsInfosByName(String goodsName) {
        Query query = new Query(Criteria.where("goodsName").regex(goodsName));
        // 仅查询goodsName和goodsId字段
        query.fields().include("goodsName").include("goodsId");
        List<GoodsInfos> goodsInfos = mongoTemplate.find(query, GoodsInfos.class);
        if (goodsInfos.size()  > 0) {
            return R.success(goodsInfos);
        } else {
            return R.success(new ArrayList<>());
        }
    }

    /**
     * 根据商品id获取商品信息
     * @param goodsId 商品id
     * @return `
     */
    @Override
    public R<GoodsInfos> getGoodsInfosById(String goodsId) {
        Query query = new Query(Criteria.where("goodsId").is(goodsId));
        GoodsInfos goodsInfos = mongoTemplate.findOne(query, GoodsInfos.class);
        if (goodsInfos != null) {
            return R.success(goodsInfos);
        }
        return R.error("商品不存在");
    }
}
